{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Assignment1: Welcome aboard!\n", "\n", "## CS240
Bryn Mawr College
Professor Blank\n", "\n", "Welcome aboard to CS240: Priciples of Computer Organization!\n", "\n", "**Due:** Wednesday, Sep 2, 2015, 2:40pm. Print out, and bring to class.\n", "\n", "This first assignment introduces a number of new things:\n", "\n", "* **Jupyter notebooks**, like this one\n", " * can enter text and code\n", "* Two Machine **instructions**:\n", " * 0001 - add\n", " * 1111 0000 00100101 - halt\n", "* Eight **Registers**: \n", " * 000, 001, 002, etc.\n", "* Assembly **directives** (start with a dot)\n", " * .ORIG\n", " * .END\n", "* **Magic**, meta-commands\n", " * %exe\n", " * %reg\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Read Chapter 1\n", "\n", "Read chapter 1, and write down 6 questions for discussion in class on Wednesday, Sep 2:\n", "\n", "* Three original questions: review-type questions, that cover the material in the chapter\n", "* Three original questions: clarifying questions, that answer a real question that you may be confused about\n", " " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Our first Machine Language Program\n", "\n", "For this, our \"Hello, World\" program in Machine Language, our goal is to:\n", "\n", "* write a machine language program\n", "* put it in memory location x3000\n", "* add 1 to the current value in the first register, 000 also known as R0\n", "* stop\n", "\n", "We create a machine language program by putting machine language code in a Jupyter cell, and pressing Shift+Enter, or pressing the triangle button at top of page. This will **assemble** the code and place it into memory.\n", "\n", "A machine language program always starts with where in memory the code should be placed, its \"origin\". We'll often use x3000 as a generally open place in memory, but it could be anywhere. We use the .ORIG directive.\n", "\n", "Then, we enter all of the machine language instructions (see next).\n", "\n", "Finally, we signal to the assembler that this is the end, using the .END directive.\n", "\n", "### Instructions\n", "\n", "For our first program, we will use two instructions: **add** and **halt**.\n", "\n", "#### Add\n", "\n", "The add instruction always starts out with 0001. Then it is followed by the destination register, as source register, the number 1, and a 5-bit binary number. \n", "\n", "In shorthand, it does this:\n", "\n", "```\n", "Destination Register <- Source Register + 5-bit binary number\n", "```\n", "\n", "#### Halt\n", "\n", "The halt instruction is long, but specific:\n", "\n", "```\n", "1111 0000 00100101\n", "```\n", "\n", "There are variations of this command, but for now, we'll just use it as it is." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Putting it all together\n", "\n", "Now we just put the .ORIG, code, and .END together in a cell, and press Shift+Enter:" ] }, { "cell_type": "code", "execution_count": 13, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "============================================================\n", "Memory dump:\n", "============================================================\n", " x3000: x1021\n", " x3001: xF025\n", "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x3002\n", "N: 0 Z: 0 P: 1 \n", "R0: x0000 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3002 \n" ] } ], "source": [ ".ORIG x3000\n", "0001 000 000 1 00001\n", "1111 0000 00100101\n", ".END" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "There, you just assembled your first program! What you are seeing is:\n", "\n", "* a \"Memory dump\" showing the two instructions, but now in hexidecimal format.\n", "* the \"registers\" and their values. R0 through r7 should all be zero.\n", "\n", "But we didn't actually run the program. To do that, we use a Jupyter Calysto LC3 magic, %exe. Enter `%exe` in a cell, and press Shift+Enter:" ] }, { "cell_type": "code", "execution_count": 14, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x048E\n", "N: 0 Z: 0 P: 1 \n", "R0: x0001 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3002 \n", "============================================================\n", "Computation completed\n", "============================================================\n", "Instructions: 2\n", "Cycles: 15 (0.000008 milliseconds)\n" ] } ], "source": [ "%exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You just ran your first machine language program! What did it do? Just what we told it:\n", "\n", "```\n", "Destination Register (R0) <- Source Register (R0) + 5-bit binary number (00001)\n", "```\n", "So, if it worked correctly, you should see R0 has the value 1." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can run it again:" ] }, { "cell_type": "code", "execution_count": 15, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x048E\n", "N: 0 Z: 0 P: 1 \n", "R0: x0002 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x3002 \n", "============================================================\n", "Computation completed\n", "============================================================\n", "Instructions: 2\n", "Cycles: 15 (0.000008 milliseconds)\n" ] } ], "source": [ "%exe" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "And now R0 contains 2.\n", "\n", "To reset the register, use:\n", "\n", "```\n", "%reg REGISTER VALUE\n", "```\n", "\n", "So, to reset register 000 to 0 use:" ] }, { "cell_type": "code", "execution_count": 17, "metadata": { "collapsed": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "============================================================\n", "Registers:\n", "============================================================\n", "PC: x048E\n", "N: 0 Z: 0 P: 1 \n", "R0: x0000 R1: x0000 R2: x0000 R3: x0000 \n", "R4: x0000 R5: x0000 R6: x0000 R7: x0000 \n" ] } ], "source": [ "%reg 000 0" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "You can continue to run it again, or set registers to different values." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Questions\n", "\n", "For the following questions, using only the add instruction, do the following:\n", "\n", "1. Create your own notebook, with your own text, and instructions\n", "2. Put the number 1 in register 3. What is three in binary?\n", "3. Put the number 2 in register 5. What is two and five in binary?\n", "4. Can you put the number 32 into register 0? How? Do it, if you can." ] } ], "metadata": { "kernelspec": { "display_name": "Calysto LC3", "language": "asm", "name": "calysto_lc3" }, "language_info": { "file_extension": ".asm", "mimetype": "text/x-gas", "name": "gas" } }, "nbformat": 4, "nbformat_minor": 0 }